<template>
  <div class="app-container">
    <!--查询表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="6">
            <el-form-item label="角色名称">
              <el-input
                style="width: 100%"
                v-model="searchObj.roleName"
                placeholder="角色名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" style="margin-left: 10px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="fetchData()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetData"
              >重置</el-button
            >
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-button
              type="success"
              icon="el-icon-plus"
              size="mini"
              @click="handleSave()"
              >添加</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-minus"
              size="mini"
              :disabled="isDisabled"
              @click="deleteBatch()"
              >批量删除</el-button
            >
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 添加/修改表单 -->
    <div class="saveForm">
      <el-dialog
        :title="sysRole.id ? '修改' : '添加'"
        :visible.sync="dialogFormVisible"
      >
        <el-form :model="sysRole">
          <el-form-item label="角色名称" :label-width="formLabelWidth">
            <el-input v-model="sysRole.roleName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色标识" :label-width="formLabelWidth">
            <el-input v-model="sysRole.roleCode" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色备注" :label-width="formLabelWidth">
            <el-input
              v-model="sysRole.description"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="sysRole.id ? update() : save()"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>
    <el-table
      v-loading="listLoading"
      :data="roleList"
      :key="itemKey"
      stripe
      border
      style="width: 100%; margin-top: 10px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="roleName" label="角色名称" />
      <el-table-column prop="roleCode" label="角色编码" />
      <el-table-column prop="description" label="备注" />
      <el-table-column prop="createTime" label="创建时间" width="160" />
      <el-table-column prop="updateTime" label="更新时间" width="160" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="handleEdit(scope.row)"
            title="修改"
          />
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeDataById(scope.row.id)"
            title="删除"
          />
          <!-- 目前有bug,没有权限的角色点击不提示  -->
          <el-button size="mini" type="info" :disabled="$hasBP('btn.sysRole.assignAuth') === false" @click="toAssign(scope.row)"
            >分配菜单</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="text-align: center"
    >
    </el-pagination>
  </div>
</template>
<script>
import api from "@/api/system/role/role";

export default {
  data() {
    return {
      roleList: [],
      total: 0,
      limit: 5,
      currentPage: 1,
      searchObj: {},
      listLoading: true,
      formLabelWidth: "100",
      itemKey: Math.random(),
      sysRole: {},
      dialogFormVisible: false,
      roleIds: [],
      isDisabled: true,
      menuList: [],
    };
  },
  created() {
    this.fetchData()
  },
  methods: {
    buttonIsDisabled() {
      if (this.roleIds.length == 0) {
        this.isDisabled = true;
      }
      if (this.roleIds.length > 0) {
        this.isDisabled = false;
      }
    },
    fetchData() {
      console.log("列表刷新:" + this.currentPage + " " + this.limit);
      api
        .getPageList(this.currentPage, this.limit, this.searchObj)
        .then((response) => {
          this.itemKey = Math.random();
          this.roleList = response.data.records;
          this.total = response.data.total;
          this.listLoading = false;
        });
    },
    resetData() {
      this.searchObj = {};
      this.fetchData();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    handleSelectionChange(val) {
      //每次监听都重新向空数组中加值,不然会造成取消的值无法从数组中取出
      this.roleIds = [];
      for (var i = 0; i < val.length; i++) {
        this.roleIds.push(val[i].id);
      }
      this.buttonIsDisabled();
    },
    removeDataById(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        api.removeByID(id).then((response) => {
          this.$message({
            type: "success",
            message: "删除成功",
          });
          this.fetchData();
        });
      });
    },
    handleSave() {
      this.dialogFormVisible = true;
    },
    handleEdit(role) {
      this.sysRole = role;
      this.dialogFormVisible = true;
    },
    save() {
      api
        .saveRole(this.sysRole)
        .then((response) => {
          this.$message({
            type: "success",
            message: "添加成功",
          });
          this.sysRole = {};
          this.fetchData();
        })
        .catch((response) => {
          this.$message({
            type: "error",
            message: "添加失败",
          });
          this.sysRole = {};
          this.fetchData();
        });
      this.dialogFormVisible = false;
    },
    update() {
      api
        .updateRole(this.sysRole)
        .then((response) => {
          this.$message({
            type: "success",
            message: "修改成功",
          });
          this.sysRole = {};
          this.fetchData();
        })
        .catch((response) => {
          this.$message({
            type: "error",
            message: "修改失败",
          });
          this.sysRole = {};
          this.fetchData();
        });
      this.dialogFormVisible = false;
    },
    deleteBatch() {
      this.$confirm("此操作将永久删除数据, 是否继续?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        api
          .batchRemove(this.roleIds)
          .then((response) => {
            this.$message({
              type: "success",
              message: "删除成功",
            });
            this.fetchData();
          })
          .catch((response) => {
            this.$message({
              type: "error",
              message: "删除失败",
            });
            this.fetchData();
          });
      });
    },
    toAssign(role) {
      this.$router.push("/system/authRole?id=" + role.id + "&roleName=" + role.roleName)
    },
  },
};
</script>